package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhentao.pojo.PatientInfo;
import com.zhentao.pojo.R;
import com.zhentao.pojo.RelationType;
import com.zhentao.service.PatientInfoService;
import com.zhentao.service.RelationTypeService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/patient")
@RequiredArgsConstructor
public class PatientController {

    private final PatientInfoService patientInfoService;
    private final RelationTypeService relationTypeService;

    // 就诊人列表（包含关系名称）
    @GetMapping("/list")
    public R<List<Map<String, Object>>> list() {
        List<PatientInfo> patients = patientInfoService.list(new LambdaQueryWrapper<PatientInfo>()
            .orderByDesc(PatientInfo::getIsDefault)
            .orderByAsc(PatientInfo::getId));

        // 关系类型映射 {id -> typeName}
        Map<Integer, String> relationIdToName = relationTypeService.list().stream()
            .collect(Collectors.toMap(RelationType::getId, RelationType::getTypeName));

        List<Map<String, Object>> result = new ArrayList<>();
        for (PatientInfo p : patients) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", p.getId());
            item.put("name", p.getName());
            item.put("relationTypeId", p.getRelationTypeId());
            item.put("relationTypeName", relationIdToName.getOrDefault(p.getRelationTypeId(), ""));
            item.put("idCardTypeId", p.getIdCardTypeId());
            item.put("idCardNumber", p.getIdCardNumber());
            item.put("medicalCardNumber", p.getMedicalCardNumber());
            item.put("phoneNumber", p.getPhoneNumber());
            item.put("address", p.getAddress());
            item.put("isDefault", p.getIsDefault());
            result.add(item);
        }
        return R.ok(result);
    }
}


